// 如果没有通过拦截器配置域名的话，可以在这里写上完整的URL(加上域名部分)
let indexUrl = '/api/index';

// 此处第二个参数vm，就是我们在页面使用的this，你可以通过vm获取vuex等操作，更多内容详见uView对拦截器的介绍部分：
// https://uviewui.com/js/http.html#%E4%BD%95%E8%B0%93%E8%AF%B7%E6%B1%82%E6%8B%A6%E6%88%AA%EF%BC%9F
const install = (Vue, vm) => {
	// 首页
	let index = (params = {}) => vm.$u.get(indexUrl, params);
	
	// 认证相关
	const authLogin = params => vm.$u.post('/api/auth/login', params)  					// 登陆
	const authRegister = params => vm.$u.post('/api/auth/register', params)  		// 注册
	const authLogout = () => vm.$u.post('/api/auth/logout')  										// 退出登陆
	const authOssToken = () => vm.$u.get('/api/auth/oss/token')  								// 退出登陆
	
	
	//用户相关
	const userInfo = () => vm.$u.get('/api/user')																// 用户信息
	const updateUserInfo = params => vm.$u.put('/api/user', params)							// 修改用户信息
	const updateUserAvatar = params => vm.$u.patch('/api/user/avatar', params)	// 修改用户头像
	
	// 商品相关
	const goodsDetail = id => vm.$u.get(`/api/goods/${id}`)											//商品详情
	const goodsList = (params = {}) => vm.$u.get('/api/goods', params)					//商品列表
	
	//收藏相关
	const goodsCollect = id => vm.$u.post(`/api/collects/goods/${id}`)					//商品收藏
	const collectList = id => vm.$u.get('/api/collects')												//我的收藏
	
	// 购物车相关
	const cartsAdd = params => vm.$u.post("/api/carts", params)																	//加入购物车
	const cartsList = (params = {}) => vm.$u.get("/api/carts", params)													//购物车列表
	const cartsCountChange = params => vm.$u.put(`/api/carts/${params.cart}`, params.data)			//购物车数量改变
	const cartsDel = cart => vm.$u.delete(`/api/carts/${cart}`)																	//移出购物车
	const cartsChecked = params => vm.$u.patch("/api/carts/checked", params)										//购物车改变选中
	
	// 收获地址相关
	const addressList = () => vm.$u.get('/api/address')																//收获地址列表
	const addressAdd = params => vm.$u.post('/api/address', params)										//添加收获地址
	const addressUpdate = params => vm.$u.put(`/api/address/${params.id}`, params)		//添加收获地址
	const addressDetail = address => vm.$u.get(`/api/address/${address}`)							//收获地址详情
	const addressDel = address => vm.$u.delete(`/api/address/${address}`)							//删除收获地址
	const addressDefault = address => vm.$u.patch(`/api/address/${address}/default`)	//为默认地址
	
	// 订单相关
	const orderList = (params = {}) => vm.$u.get('/api/orders', params)								//订单列表
	const orderPrew = () => vm.$u.get('/api/orders/preview')													//订单预览
	const orderAdd = (params) => vm.$u.post('/api/orders', params)										//提交订单
	const orderDetail = (order) => vm.$u.get(`/api/orders/${order}`)									//订单详情
	
	// 将各个定义的接口名称，统一放进对象挂载到vm.$u.api(因为vm就是this，也即this.$u.api)下
	vm.$u.api = {
		index,
		authLogin,
		userInfo,
		authRegister,
		authLogout,
		updateUserInfo,
		authOssToken,
		updateUserAvatar,
		goodsDetail,
		goodsCollect,
		goodsList,
		collectList,
		cartsAdd,
		cartsList,
		addressList,
		addressAdd,
		addressUpdate,
		addressDel,
		addressDefault,
		addressDetail,
		cartsCountChange,
		cartsDel,
		cartsChecked,
		orderList,
		orderPrew,
		orderAdd,
		orderDetail
	};
}

export default {
	install
}